package com.songcha.sakura.ui.activity

import android.Manifest
import android.app.Activity
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.*
import android.view.KeyEvent
import android.view.View
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import com.songcha.library_base.base.BaseActivity
import com.songcha.library_base.base.IOnInputUnFocus
import com.songcha.library_common.util.ToastUtil
import com.songcha.sakura.databinding.ActivityMainBinding
import com.songcha.sakura.mvvm.local.downloaad.LocalDownloadFragment
import com.songcha.sakura.mvvm.home.home.HomeFragment
import com.songcha.sakura.mvvm.setting.SettingFragment
import com.songcha.sakura.download.DownloadServiceController
import com.songcha.sakura.mvvm.novel.NovelFragment
import com.songcha.sakura.mvvm.sniff.SniffFragment
import com.songcha.sakura.util.MyJumpUtil
import com.songcha.library_common.ui.view.BottomNavigationView
import java.util.*
import kotlin.system.exitProcess


class MainActivity : BaseActivity(), BottomNavigationView.IOnBottomNavigationItemChangeListener {
    private lateinit var binding: ActivityMainBinding
    // 是否退出程序
    private var isExit = false
    // 定时触发器
    private var tExit: Timer? = null
/*
    val mFragments=listOf<Fragment>(HomeFragment(),
        LocalDownloadFragment(), SniffFragment(), SettingFragment()
    )*/

    val mFragments=listOf<Fragment>(
        HomeFragment(),
        LocalDownloadFragment(),
        NovelFragment(),
        SniffFragment(),
        SettingFragment(),
    )



    override fun getRootView() :View{
        binding = ActivityMainBinding.inflate(layoutInflater)
        return binding.root
    }



    override fun initView(savedInstanceState: Bundle?){
        //init_(savedInstanceState)
        val vp=binding.nsvp
        vp.adapter=object:FragmentStatePagerAdapter(supportFragmentManager){
            override fun getCount(): Int {
                return mFragments.size
            }

            override fun getItem(position: Int): Fragment {
                return mFragments[position]
            }

        }

        binding.bnv.setViewPager(vp)
        binding.bnv.setOnBottomNavigationItemChangeListener(this)

        requestPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)

        intent.extras?.let {
            val b=it.getBoolean("enterByNotification")
            if(b){
                DownloadServiceController.getTaskList()
            }
        }

        MyJumpUtil.toActivity(this,SplashActivity::class.java)
        //Looper.loop()
    }


    override fun getStatusBarColor(): Int {
        return Color.WHITE
    }


    private fun requestPermission(context: Activity,permission:String){
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(context, arrayOf(permission), 1);
            }
        }
    }

    override fun onResume() {
        super.onResume()
        //binding.bnv.selectCurrent()
    }


    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isExit === false) {
                isExit = true
                if (tExit != null) {
                    tExit!!.cancel() // 将原任务从队列中移除
                }
                // 重新实例一个定时器
                tExit = Timer()
                val task: TimerTask = object : TimerTask() {
                    override fun run() {
                        isExit = false
                    }
                }
                ToastUtil.show("再按一次退出程序")
                // 延时两秒触发task任务
                tExit?.schedule(task, 2000)
            } else {
                finish()
                exitProcess(0)
            }
            return true
        }
        return super.onKeyUp(keyCode, event)
    }

    override fun onBottomNavigationItemChange(position: Int,tag:String) {
        for(frag in mFragments){
            if(frag is BottomNavigationView.IOnBottomNavigationItemChangeListener)
                frag.onBottomNavigationItemChange(position,tag)
        }
    }

    override fun onInputUnFocus() {
        for(frag in mFragments){
            if(frag is IOnInputUnFocus)
                frag.onInputUnFocus()
        }
    }


}